var express = require('express');
var users = require('./users');
var characters = require('./characters');
var classes = require('./classes');

//game main
var io = require('socket.io').listen(3366);

chars = {};

io.sockets.on('connection', function (socket) {

    socket.on('join', function(sid){
        users.getCharacter(sid, function(err, character){
            if(!err){
                if(character){
                    for (var prop in chars) {
                        socket.emit('newchar', chars[prop]);
                    }
                    character.id = socket.id;
                    character.x = Math.floor(Math.random()*401);
                    character.y = Math.floor(Math.random()*401);
                    if(character.kills == undefined){
                        character.kills = 0;
                    }
                    if(character.deaths == undefined){
                        character.deaths = 0;
                    }

                    socket.emit('mychar', character);
                    socket.broadcast.emit('newchar', character);
                    chars[socket.id] = character;
                }else{
                    socket.emit('mesg', 'no character selected');
                } 
            }else{
                if(err.message == "already connected"){
                    socket.emit('mesg', 'already connected');
                }else{
                    console.log(err);
                }
            }
        });
    
    });
    
    socket.on('move', function(loc){
        if(chars[socket.id]){
            chars[socket.id].x = loc.x;
            chars[socket.id].y = loc.y;
        
            socket.emit(socket.id, {
                cmd:"move",
                x:loc.x,
                y:loc.y
            });
            socket.broadcast.emit(socket.id, {
                cmd:"move",
                x:loc.x,
                y:loc.y
            });
        }
    });
    
    socket.on('atk', function(id){
        var dchar = chars[socket.id];
        var target = chars[id];
        if ((Math.abs(dchar.x - target.x) + Math.abs(dchar.y - target.y)) < 30.0) {
            socket.emit(socket.id, {
                cmd:"die",
                id:id
            });
            socket.broadcast.emit(socket.id, {
                cmd:"die",
                id:id
            });
            
            dchar.kills++;
            target.deaths++;
            
            socket.emit('scores', {
                name:dchar.name,
                kills:dchar.kills,
                deaths:dchar.deaths,
                name2:target.name,
                kills2:target.kills,
                deaths2:target.deaths
            });
            socket.broadcast.emit('scores', {
                name:dchar.name,
                kills:dchar.kills,
                deaths:dchar.deaths,
                name2:target.name,
                kills2:target.kills,
                deaths2:target.deaths
            });
            
        //            characters.save(target);
        //            delete chars[id];
        }
    });
    
    socket.on('disconnect', function(){
        if(chars[socket.id]){

            users.disconnect(chars[socket.id].owner);
            characters.save(chars[socket.id]);
            delete chars[socket.id];
            socket.broadcast.emit('delchar', socket.id);

        }
    });
});

//server game world;

function Character(x, y){
    this.x = x;
    this.y = y;
    this.destX = x + 0.2;
    this.destY = y + 0.2;
    this.name = "";
    this.hp = 15;
    this.mp = 15;
    this.sp = 15
    this.maxHp = 15;
    this.maxMp = 15;
    this.maxSp = 15;
    this.active = true;
    this.theta = 0;
}

Character.prototype.update = function(delta){
    if(this.active){
        var deltaX = this.destX - this.x;
        var deltaY = this.destY - this.y;
        
        if ((Math.abs(deltaX) + Math.abs(deltaY)) > 2.0) {
            var theta = Math.atan2(deltaX, deltaY);
            this.x += (this.speed * Math.sin(theta) * delta);
            this.y += (this.speed * Math.cos(theta) * delta);
        }
    }
}

function Stage() {
    this.targetFps = 40;
    this.targetInterval = Math.round(1000 /  this.targetFps);
    this.characters = {};
    this.timer = -1;
    this.currentTime = -1;
}

Stage.prototype.start = function() {
    this.currentTime = Date.now();
    var self = this;
    this.timer = setInterval(function() {
        var delta = Date.now() - self.currentTime;
        self.currentTime = Date.now();
        self.update(delta);
    }, this.targetInterval);
};

Stage.prototype.addCharacter = function(id, character) {
    this.characters[id] = character;
};

Stage.prototype.delCharacter = function(id) {
    delete this.characters[id];
};

Stage.prototype.update = function(delta) {
    for(var i in this.characters){
        this.characters[i].update(delta);
    }
};

var stage = new Stage();
stage.start();

